﻿#include "TrendChartSetting.h"
#include "ui_TrendChartWidget.h"
#include "qcolordialog.h"
#include "QCustomPlot/QTrendChart.h"

class TrendChartSetting::Internal
{
public:
	Ui::TrendChartWidget ui;
	TrendChartParam*  paramPtr;
	QTrendChart* graph = nullptr;
};

TrendChartSetting::TrendChartSetting(QWidget* parent) :
	QDialog(parent),
	p(new Internal())
{
	p->ui.setupUi(this);
}

TrendChartSetting::~TrendChartSetting()
{
	delete p;
	p = nullptr;
}

void TrendChartSetting::inputParam(TrendChartParam* paramPtr, QTrendChart* graph)
{
	p->paramPtr = paramPtr;
	p->graph = graph;
}

void TrendChartSetting::on_btnWaveLineColor_colorChanged(const QColor& color)
{
	
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->waveLineColor = color.name().toUtf8();

		if (p->graph != nullptr)
		{
			p->graph->setWaveLine(p->paramPtr->name, color);
		}
	}
}

void TrendChartSetting::on_btnLSLLineColor_colorChanged(const QColor& color)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->LSL_Color = color.name().toUtf8();

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 1, color, p->paramPtr->LSL_Value);
		}
	}
}

void TrendChartSetting::on_btnUSLLineColor_colorChanged(const QColor& color)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->USL_Color = color.name().toUtf8();

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 2, color, p->paramPtr->USL_Value);
		}
	}
}

void TrendChartSetting::on_btnStandardLineColor_colorChanged(const QColor& color)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->StandardColor = color.name().toUtf8();

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 3, color, p->paramPtr->StandardValue);
		}
	}
}

void TrendChartSetting::on_doubleSpinBox_yMinValue_valueChanged(double d)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->yAxisMin = d;

		if (p->graph != nullptr)
		{
			p->graph->yAxis->setRange(p->paramPtr->yAxisMin,p->paramPtr->yAxisMax);
		}
	}
}

void TrendChartSetting::on_doubleSpinBox_yMAxValue_valueChanged(double d)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->yAxisMax = d;

		if (p->graph != nullptr)
		{
			p->graph->yAxis->setRange(p->paramPtr->yAxisMin, p->paramPtr->yAxisMax);
		}
	}
}

void TrendChartSetting::on_doubleSpinBox_specLowValue_valueChanged(double d)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->LSL_Value = d;

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 1, QColor(p->paramPtr->LSL_Color), p->paramPtr->LSL_Value);
		}
	}
}

void TrendChartSetting::on_doubleSpinBox_specHightValue_valueChanged(double d)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->USL_Value = d;

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 2, QColor(p->paramPtr->USL_Color), p->paramPtr->USL_Value);
		}
	}
}

void TrendChartSetting::on_doubleSpinBox_specStandardValue_valueChanged(double d)
{
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->StandardValue = d;

		if (p->graph != nullptr)
		{
			p->graph->setReferenceLine(p->paramPtr->name, 3, QColor(p->paramPtr->StandardColor), p->paramPtr->StandardValue);
		}
	}
}

void TrendChartSetting::on_btnBGColor_colorChanged(const QColor& color)
{
	
	if (p->paramPtr != nullptr)
	{
		p->paramPtr->bgColor = color.name();
		if (p->graph != nullptr)
		{
			p->graph->setBackground(color);
		}
	}
}


bool TrendChartSetting::event(QEvent* event)
{
	if (event->type() == QEvent::Show)
	{
		
		if (p->paramPtr != nullptr)
		{
			p->ui.btnLSLLineColor->setColor(QColor(p->paramPtr->LSL_Color));
			p->ui.btnBGColor->setColor(QColor(p->paramPtr->bgColor));
			p->ui.btnUSLLineColor->setColor(QColor(p->paramPtr->USL_Color));
			p->ui.btnWaveLineColor->setColor(QColor(p->paramPtr->waveLineColor));
			p->ui.btnStandardLineColor->setColor(QColor(p->paramPtr->StandardColor));
			
			p->ui.doubleSpinBox_yMinValue->setValue(p->paramPtr->yAxisMin);
			p->ui.doubleSpinBox_yMAxValue->setValue(p->paramPtr->yAxisMax);
			p->ui.doubleSpinBox_specLowValue->setValue(p->paramPtr->LSL_Value);
			p->ui.doubleSpinBox_specHightValue->setValue(p->paramPtr->USL_Value);
			p->ui.doubleSpinBox_specStandardValue->setValue(p->paramPtr->StandardValue);
		}
	}
	return __super::event(event);
}